<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/6/15
 * Time: 16:01
 */

namespace Admin\Model;

use Think\Model;
use Think\Page;

class OrderModel extends Model{
    protected $tableName = "order_hospital";
    protected $userTableName = "user";
    public function _initialize(){
        import('ORG.Util.Page');// 导入分页类

        //加上表前缀
        $this->userTableName = C('DB_PREFIX').$this->userTableName;
    }

    public function getAllList($attend_type,$hid){
        $count = $this->where("attend_type = %d and hid = %d",[$attend_type,$hid])->count();
        $Page = new Page($count,5);// 实例化分页类 传入总记录数
        $show = $Page->show();// 分页显示输出
        $list = $this->where("attend_type = %d and hid = %d",[$attend_type,$hid])->limit($Page->firstRow.','.$Page->listRows)->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        return [
            'list' => $list,
            'show' => $show
        ];
    }

    public function getSelectList($data){
        $hid = $data['hid'];
        $attend_type = $data['attend_type'];
        $order_status = $data['order_status'];
        $pay_status = $data['pay_status'];
        $start_time = $data['start_time'];
        $end_time = $data['end_time'];
        $order_type = $data['order_type'];
        $pay_type = $data['pay_type'];
        $keyword = $data['keyword'];
        $where = "attend_type = $attend_type and hid = $hid";
        if($order_status !== null){
            $where .= " and ";
            $where .= "order_status = $order_status";
        }
        if($pay_status !== null){
            $where .= " and ";
            $where .= "pay_status = $pay_status";
        }
        if($start_time !== ''){
            $where .= " and ";
            $where .= "start_time >= ".strtotime($start_time);
        }
        if($end_time !== ''){
            $where .= " and ";
            $where .= "start_time <= ".strtotime($end_time);
        }
        if($order_type !== null){
            $where .= " and ";
            $where .= "order_type = $order_type";
        }
        if($pay_type !== null){
            $where .= " and ";
            $where .= "pay_type = $pay_type";
        }
        if($keyword !== ''){
            $where .= " and ";
            $where .= "order_number like '%$keyword%' or mobile like '%$keyword%' or o.truename like '%$keyword%'";
        }
        $count = M('order_hospital as o')
            ->join("left join $this->userTableName as u on u.id = o.uid")
            ->where($where)
            ->count();
        $Page = new Page($count,5);// 实例化分页类 传入总记录数
        $show = $Page->show();// 分页显示输出
        $list = M('order_hospital as o')
            ->where($where)
            ->limit($Page->firstRow.','.$Page->listRows)
            ->join("left join $this->userTableName as u on u.id = o.uid")
            ->select();
        return [
            'list' => $list,
            'show' => $show
        ];
    }

    public function getOrder($id){
        $id || die();
        $res = M($this->tableName." as h")
            ->field("h.*,u.sex as sex")
            ->join('join wp_user as u on u.id = h.uid')
            ->where("h.id = %d",$id)
            ->find();
        return $res;
    }
}